package com.jams.goodox.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.jams.goodox.R;


public class SortView extends View{

	// 注册监听触摸改变
	private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
	private String[] b;
	// 设定右边栏只有26个大些字母加上#
	/*private String[] b = {"A", "B", "C", "D", "E", "F", "G", "H", "I",
			"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
			"W", "X", "Y", "Z" ,"#"};*/
	// 选中的字符下标
	private int choose = -1;
	// 字符画笔
	Paint paint = new Paint();

	

	public SortView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public SortView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public SortView(Context context, Object[] b1) {
		super(context);
		if (b1 != null && b1.length > 0) {
			b = new String[b1.length];
			for (int i = 0; i < b1.length; i++) {
				b[i] = (String) b1[i];
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		int height = getHeight(); // 获取当前控件高度
		int width = getWidth(); // 获取当前控件宽度
		// 设定显示每个字符的高度
		int singleHeight = height / b.length;
		for (int i = 0; i < b.length; i++) {
			//float fontScale = getContext().getResources().getDisplayMetrics().density;
			//paint.setColor(getContext().getResources().getColor(R.color.text_blue));
			//System.out.println(fontScale);
			//paint.setTextSize(30.0f);
			//paint.setTypeface(Typeface.DEFAULT_BOLD);
			//paint.setAntiAlias(true);
			
			float fontScale = getContext().getResources().getDisplayMetrics().density;
			paint.setColor(getResources().getColor(R.color.main_theme));
			paint.setTextSize(sp2px(13, fontScale));
			paint.setTypeface(Typeface.DEFAULT_BOLD);
			paint.setAntiAlias(true);

			

			// 已选中的字符设定不同颜色
			if (i == choose) {
				paint.setColor(Color.RED);
				paint.setFakeBoldText(true);
			}

			float xPos = width / 2 - paint.measureText(b[i]) / 2;
			float yPos = singleHeight * i + singleHeight;
			canvas.drawText(b[i], xPos, yPos, paint);
			paint.reset();
		}
	}

	public static int sp2px(float spValue, float fontScale) {
		return (int) (spValue * fontScale + 0.5f);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = choose;
		final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		// 获取当前选中的字符下标
		final int c = (int) (y / getHeight() * b.length);

		switch (action) {
		case MotionEvent.ACTION_DOWN:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c]);
					choose = c;
					invalidate();
				}
			}

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c && listener != null) {
				if (c >= 0 && c < b.length) {
					listener.onTouchingLetterChanged(b[c]);
					choose = c;
					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			choose = -1;
			invalidate();
			break;
		}
		return true;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	public void setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	/**
	 * 监听触摸改变
	 * 
	 * @author sport
	 * 
	 */
	public interface OnTouchingLetterChangedListener {
		/**
		 * 当前选中的字符
		 * 
		 * @param s
		 * @return void
		 */
		public void onTouchingLetterChanged(String s);
	}

}
